package com.yjlove.myplay.ui.activity;

import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.squareup.okhttp.Request;
import com.yjlove.myplay.R;
import com.yjlove.myplay.base.BaseActivity;
import com.yjlove.myplay.bean.NetFileInfo;
import com.yjlove.myplay.common.StringNetUrl;
import com.yjlove.myplay.common.StringPath;
import com.yjlove.myplay.jsinter.JavaScriptInterface;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.FileCallBack;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/30.
 */
public class WebViewTestActivity extends BaseActivity{

    private static String TAG = "WebViewTestActivity";
    private static final int SUCCESS = 1;
    private static final int ERROR = 0;
    private WebView mWebView;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            //super.handleMessage(msg);
            switch (msg.what){
                case ERROR:
                    mWebView.loadUrl("http://www.baidu.com");
                    break;
                case SUCCESS:
                    mWebView.loadUrl("file:///" + StringPath.HTML_PATH + "testHtml.html");
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected String getActivityTitle() {
        return getIntent().getStringExtra("title");
    }

    @Override
    protected int getActivityView() {
        return R.layout.activity_wvtest;
    }

    protected void initView() {
        mWebView = (WebView) findViewById(R.id.webview);
    }

    protected void initData() {

        List<NetFileInfo> mNetFileList = new ArrayList<>();
        mNetFileList.add(new NetFileInfo(StringPath.HTML_PATH, "testHtml.html", StringNetUrl.WEBWIEW_TEST_HTML));
        mNetFileList.add(new NetFileInfo(StringPath.HTML_I_PATH, "banner01.jpg", StringNetUrl.WEBWIEW_I_BANNER01));
        mNetFileList.add(new NetFileInfo(StringPath.HTML_I_PATH, "banner02.jpg", StringNetUrl.WEBWIEW_I_BANNER02));
        mNetFileList.add(new NetFileInfo(StringPath.HTML_I_PATH, "banner03.jpg", StringNetUrl.WEBWIEW_I_BANNER03));
        mNetFileList.add(new NetFileInfo(StringPath.HTML_I_PATH, "UAV1.jpg", StringNetUrl.WEBWIEW_I_UAV01));
        mNetFileList.add(new NetFileInfo(StringPath.HTML_I_PATH, "UAV2.jpg", StringNetUrl.WEBWIEW_I_UAV02));
        mNetFileList.add(new NetFileInfo(StringPath.HTML_I_PATH, "UAV3.jpg", StringNetUrl.WEBWIEW_I_UAV03));

        //声明WebSettings子类
        WebSettings webSettings = mWebView.getSettings();

        //如果访问的页面中要与Javascript交互，则webview必须设置支持Javascript
        webSettings.setJavaScriptEnabled(true);

        //设置自适应屏幕，两者合用
        webSettings.setUseWideViewPort(true); //将图片调整到适合webview的大小
        webSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小

        //缩放操作
        webSettings.setSupportZoom(true); //支持缩放，默认为true。是下面那个的前提。
        webSettings.setBuiltInZoomControls(true); //设置内置的缩放控件。若为false，则该WebView不可缩放
        webSettings.setDisplayZoomControls(false); //隐藏原生的缩放控件

        //其他细节操作
        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); //关闭webview中缓存
        webSettings.setAllowFileAccess(true); //设置可以访问文件
        //webSettings.setJavaScriptCanOpenWindowsAutomatically(true); //支持通过JS打开新窗口
        webSettings.setLoadsImagesAutomatically(true); //支持自动加载图片
        webSettings.setDefaultTextEncodingName("utf-8");//设置编码格式

        mWebView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });

        //mWebView.loadUrl(StringNetUrl.WEBWIEW_YJLOVE);
        //mWebView.loadUrl(StringNetUrl.WEBWIEW_TEST_HTML);
        //mWebView.loadUrl("file:///android_asset/index.html");
        //mWebView.loadUrl("content://" + StringPath.HTML_PATH + "testHtml.html");
        //String html = "html5/html/testHtml.html";
        //mWebView.loadDataWithBaseURL("file:///android_asset/", html, "text/html", "UTF-8", null);

        JavaScriptInterface JSInterface = new JavaScriptInterface(WebViewTestActivity.this);
        mWebView.addJavascriptInterface(JSInterface, "JSInterface");

        for (NetFileInfo info : mNetFileList){
            OkHttpUtils.get().url(info.getDestFileUrl())
                    .build()
                    .execute(new FileCallBack(info.getDestFileDir(), info.getDestFileName()) {
                        @Override
                        public void inProgress(float progress) {
                            //mProgressBar.setProgress((int) (100 * progress));
                        }

                        @Override
                        public void onError(Request request, Exception e) {
                            Log.e(TAG, "onError :" + e.getMessage());
                            mHandler.sendEmptyMessage(ERROR);
                        }

                        @Override
                        public void onResponse(File response) {
                            Log.e(TAG, "onResponse :" + response.getAbsolutePath());
                            mHandler.sendEmptyMessage(SUCCESS);
                            //mWebView.loadUrl("file:///" + StringPath.HTML_PATH + "testHtml.html");
                        }

                    });
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack()) {
            mWebView.goBack();// 返回前一个页面
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
